<?php

declare(strict_types=1);

namespace app\admin\model;

use app\admin\model\BaseModel;

/**
 * @mixin \think\Model
 */
class Serialnumber extends BaseModel
{
    protected $table = 'db_serialnumber';

    protected $pk = 'sn_id';

    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $dateFormat = 'Y-m-d H:i:s';
    protected $auto = ['create_time', 'creator'];
    protected $insert = ['create_time', 'creator'];

    // 控制状态获取器是否启用
    private $useStatusConverter = true;

    protected $schema = [
        'sn_id' => 'string',
        'seller_id' => 'int',
        'pid' => 'int',
        'wid' => 'int',
        'status' => 'int',
        'creator' => 'int',
        'create_time' => 'datetime',
    ];

    protected $attributes = [
        'status' => [
            0 => '未使用',
            1 => '已使用',
            2 => '已入库',
            3 => '已出库',
            4 => '维修中',
            -1 => '被废弃'
        ],
        'action' => [
            2 => '入库',
            3 => '出库',
            4 => '维修',
            5 => '出厂'
        ]
    ];

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-06-09 13:06:22 
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum($field = 'all')
    {
        return $this->getEnum(strtoupper($field), [], 'label');
    }

    /**
     * @describe 状态获取器 - 可控制是否转换
     * @author: Bobo
     * @date: 2025-06-09 15:17:07 
     * @param [type] @required
     * @return Renderable
     */
    public function getStatusAttr($value)
    {
        // 如果设置了不转换标志，直接返回原始值
        if (!$this->useStatusConverter) {
            return $value;
        }

        return $this->attributes['status'][$value] ?? $value;
    }

    /**
     * 获取原始状态值（不经过获取器转换）
     */
    public function getRawStatus()
    {
        return $this->getData('status');
    }

    /**
     * 获取状态文本描述
     */
    public function getStatusText()
    {
        $status = $this->getData('status');
        return $this->attributes['status'][$status] ?? '未知状态';
    }

    /**
     * 设置是否使用状态获取器
     */
    public function useStatusGetter($use = true)
    {
        $this->useStatusConverter = $use;
        return $this;
    }

    /**
     * 获取状态和操作映射数据
     */
    public function getAttributesMap()
    {
        return $this->attributes;
    }

    public function product()
    {
        return $this->hasOne(Product::class, 'id', 'pid')->bind([
            'product_name' => 'cname',
            'product_power' => 'power',
            'product_hashrate' => 'hashrate',
            'product_computing_unit' => 'computing_unit',
            'product_algorithm' => 'algorithm',
        ]);
    }

    public function seller()
    {
        return $this->hasOne(SellerInfo::class, 'id', 'seller_id')->bind(['seller_name' => 'name']);
    }

    public function warehouse()
    {
        return $this->hasOne(Warehouse::class, 'id', 'wid')->bind(['warehouse_name' => 'name']);
    }

    public function operator()
    {
        return $this->hasOne(Operator::class, 'id', 'creator')->bind(['creator_name' => 'nickname']);
    }

    public function whScanlog()
    {
        return $this->hasMany(WhScanlog::class, 'sn_id', 'sn_id');
    }
}
